package com.example.guiguiapplicatio.fragment.examination.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.databinding.FragmentExaminationStartItemBinding;
import com.example.guiguiapplicatio.fragment.examination.placeholder.ExaminationSigningPlaceholderContent.PlaceholderItem;

import java.util.List;

public class ExaminationStartAdapter extends RecyclerView.Adapter<ExaminationStartAdapter.ViewHolder> {
    private final List<PlaceholderItem> mValues;
    private Context mContext;

    public ExaminationStartAdapter(List<PlaceholderItem> mValues, Context mContext) {
        this.mValues = mValues;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(FragmentExaminationStartItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PlaceholderItem currentItem = mValues.get(position);
        holder.binding.examinationName.setText(currentItem.getName());
        holder.binding.examinationAge.setText(String.valueOf(currentItem.getAge()));
        holder.binding.examinationSex.setText(currentItem.getSex());
        holder.binding.examinationPeopleType.setText(String.join(",", currentItem.getListenername()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String idCard = currentItem.getIdcard();
                String name=currentItem.getName();
                Bundle bundle = new Bundle();
                bundle.putString("idCard", idCard);
                bundle.putString("name",name);
                // 使用 NavController 导航到目标 fragment，并将 Bundle 作为参数传递
                Navigation.findNavController(v).navigate(R.id.action_examinationStartFragment_to_examinationEditFragment, bundle);
            }
        });
    }


    @Override
    public int getItemCount() {
        int itemCount = mValues.size();
        return itemCount;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        FragmentExaminationStartItemBinding binding;

        public ViewHolder(@NonNull FragmentExaminationStartItemBinding fragmentExaminationStartItemBinding) {
            super(fragmentExaminationStartItemBinding.getRoot());
            this.binding = fragmentExaminationStartItemBinding;
        }
    }
}
